/**
 * Created by yuris on 16.02.15.
 */
package view {
import InterfaceObjects.AbstractTabWindow;
import InterfaceObjects.OrdetProductWindow.OrderProductItem;
import InterfaceObjects.OrdetProductWindow.OrderProductWindow;

import com.greensock.TweenMax;
import com.greensock.easing.Back;

import flash.geom.Point;

import messages.ModelToViewMessage;
import messages.ViewToControllerMessage;

import model.Constants;

import model.prototypes.ProductProto;

import mvcexpress.mvc.Mediator;

import starling.events.Event;

public class OrderProductWindowMediator extends Mediator {
    [Inject]
    public var orderProductWindow:OrderProductWindow;

    override protected function onRegister():void {
        addHandler(ModelToViewMessage.MODE_CHANGED, HANDLER_modeChanged);

        addHandler(ModelToViewMessage.NEW_PRODUCTS_AVAILABLE, HANDLER_newProductsAvailable);
        orderProductWindow.addEventListener(AbstractTabWindow.EVENT_CONTENT_ITEM_CLICK, HANDLER_contentItemClick);
        orderProductWindow.closeButton.addEventListener(Event.TRIGGERED, HANDLER_closeButtonTriggered);
    }

    private function HANDLER_modeChanged(data:Object):void {
        switch (data.mode){
            case Constants.MODE_SIMULATION_ORDER_PRODUCT_WINDOW:
                    this._showWindow();
                break;
            case Constants.MODE_SIMULATION:
                    this._hideWindow();
                break;
        }
    }

    private function _showWindow():void {
        this.orderProductWindow.alpha = 0;
        this.orderProductWindow.touchable = false;
        this.orderProductWindow.visible = true;
        if(this.orderProductWindow.currentTabNum == 0){
            this.orderProductWindow.showTab(1);
        }
        TweenMax.to(this.orderProductWindow,0.5,{y:350,height:540,alpha:1,touchable:true, ease:Back.easeOut});
    }
    private function _hideWindow():void {
        this.orderProductWindow.touchable = false;
        TweenMax.to(this.orderProductWindow,0.3,{delay:0,height:150, alpha:0,visible:false, ease:Back.easeIn});
    }

    private var _newProducts:Vector.<ProductProto>;
    private var _bufProto:ProductProto;
    private function HANDLER_newProductsAvailable(param:Object):void {
        this._newProducts = param.products
        for (var i:int = 0; i < this._newProducts.length; i++) {
            this._bufProto = this._newProducts[i];
            orderProductWindow.addTabContentSingleData(uint(this._bufProto.group),
                    {id:this._bufProto.productId ,icon: Constants.PATH_PRODUCT_ICONS_SPRITES+"/icon_"+this._bufProto.spriteId, description: "test description"})

        }
    }

    private function HANDLER_closeButtonTriggered(event:Event):void {
        sendMessage(ViewToControllerMessage.INTERFACE_LAYER_TOUCH, {type:Constants.TOUCH_ORDER_PRODUCT_CLOSE_BUTTON});
    }

    private var _orderProductItemBuf:OrderProductItem
    private var _bufPoint:Point;
    private function HANDLER_contentItemClick(event:Event):void {
        this._orderProductItemBuf = event.data.item as OrderProductItem;
        this._bufPoint = orderProductWindow.scrollContainer.localToGlobal(new Point(this._orderProductItemBuf.x,this._orderProductItemBuf.y));
        this._bufPoint.y -= orderProductWindow.scrollContainer.verticalScrollPosition;
        this._bufPoint.y += this._orderProductItemBuf.height/2;
        this._bufPoint.x += 50;
        sendMessage(ViewToControllerMessage.INTERFACE_LAYER_TOUCH, {type:Constants.TOUCH_ORDER_PRODUCT_WINDOW_ITEM, id:this._orderProductItemBuf.id,
                                                                    item_pos:this._bufPoint})
    }
    override protected function onRemove():void {

    }
}
}
